Post

Replies

Boosts

Views

Activity

Reply to Metal 4 & Acceleration Structures
Hi again and thank you for your thoughts, I am using an M1 Mac Studio. I have included as simple and linear code sample as possible, borrowing the bare minimum geometry from a sample project. I get the error message mentioned earlier at the last line: let commandBuffer: MTL4CommandBuffer = device.makeCommandBuffer()! let commandAllocator: MTL4CommandAllocator = device.makeCommandAllocator()! commandBuffer.beginCommandBuffer( allocator: commandAllocator ) let commandEncoder: MTL4ComputeCommandEncoder = commandBuffer.makeComputeCommandEncoder()! // Create a Plane Object let planeVertices: [vector_float3] = [ vector_float3( -4.5, -1.1, 0.0 ), vector_float3( -4.5, -1.1, 5.0 ), vector_float3( 4.5, -1.1, 5.0 ), vector_float3( 4.5, -1.1, 0.0 ) ] var vertexIndices: [UInt16] var vertexPositions: [vector_float3] let v0 = planeVertices[0] let v1 = planeVertices[1] let v2 = planeVertices[2] let v3 = planeVertices[3] vertexIndices = [ 0, 1, 2, 0, 2, 3 ] vertexPositions = [ v0, v1, v2, v3 ] let indexBuffer = device.makeBuffer( length: MemoryLayout<UInt16>.stride * vertexIndices.count, options: .storageModeShared )! var ptr = indexBuffer.contents() let indexPointer = ptr.bindMemory( to: UInt16.self, capacity: 1 ) for i in 0..<( vertexIndices.count ) { indexPointer[i] = vertexIndices[i] } let indexBufferRange = MTL4BufferRangeMake( indexBuffer.gpuAddress, UInt64(indexBuffer.length ) ) let positionBuffer = device.makeBuffer( length: MemoryLayout<vector_float3>.stride * vertexPositions.count, options: .storageModeShared )! ptr = positionBuffer.contents() let positionPointer = ptr.bindMemory( to: vector_float3.self, capacity: 1 ) for i in 0..<( vertexPositions.count ) { positionPointer[i] = vertexPositions[i] } let positionBufferRange = MTL4BufferRangeMake( positionBuffer.gpuAddress, UInt64( positionBuffer.length ) ) let geometryDescriptor: MTL4AccelerationStructureTriangleGeometryDescriptor = MTL4AccelerationStructureTriangleGeometryDescriptor() geometryDescriptor.indexBuffer = indexBufferRange geometryDescriptor.indexType = MTLIndexType.uint16 geometryDescriptor.vertexBuffer = positionBufferRange geometryDescriptor.vertexStride = MemoryLayout<vector_float3>.stride geometryDescriptor.triangleCount = vertexIndices.count / 3 let accelerationDescriptor: MTL4PrimitiveAccelerationStructureDescriptor = MTL4PrimitiveAccelerationStructureDescriptor() accelerationDescriptor.geometryDescriptors = [geometryDescriptor] let accelerationStructureSizes = device.accelerationStructureSizes( descriptor: accelerationDescriptor ) let accelerationStructure = (device.makeAccelerationStructure( size: accelerationStructureSizes.accelerationStructureSize ))! let scratchBuffer = device.makeBuffer( length: accelerationStructureSizes.buildScratchBufferSize, options: .storageModePrivate ) let scratchBufferRange = MTL4BufferRangeMake( scratchBuffer!.gpuAddress, UInt64( scratchBuffer!.length ) ) commandEncoder.build( destinationAccelerationStructure: accelerationStructure, descriptor: accelerationDescriptor, scratchBuffer: scratchBufferRange )
Topic: Graphics & Games SubTopic: Metal Tags:
Sep ’25
Reply to Metal 4 & Acceleration Structures
Thanks for your reply. My first issue came up when trying to use a MTL4ComputeCommandEncoder to build an AccelerationStructure: func build(destinationAccelerationStructure: any MTLAccelerationStructure, descriptor: MTL4AccelerationStructureDescriptor, scratchBuffer: MTL4BufferRange) The setup of the scratch buffer ( MTL4BufferRangeMake(::) ) is unfamiliar to me although I have looked through the documentation I am accustomed to using the MTLDevice and Acceleration Structure Sizes to build an old-school scratch buffer. Any naive attempts to use the gpuAddress value from such an existing MTLBuffer have predictably failed with the message: __buildAccelerationStructure:descriptor:scratchBuffer:]:2450: failed assertion `Compute Command Encoder Build Acceleration Structure Validation Metal 4 does not support raytracing with software emulation __ So, I guess my initial question is how do I properly create a MTL4BufferRange to use in the build function?
Topic: Graphics & Games SubTopic: Metal Tags:
Jul ’25
Reply to MetalFX upscaling sample code?
I concur with the above request - MetalFX will be such an important framework going forward ( MTLFXTemporalScaler in particular ). The Boost performance with MetalFX Upscaling video was great, but there will still be some subtle choices to be made when setting up the textures. I've implemented MPSSVGFDenoiser ( following WWDC 2019 ) which has been incredibly useful, and I relied heavily on adapting the sample code. Thanks so much!
Topic: Graphics & Games SubTopic: General Tags:
Jul ’22
Reply to Metal Shader language : float2 screws everything up.
Alignment really can cause issues when setting up buffers. Also, as was mentioned earlier, using a bridging header can give you a lot of control over the sizes of structs that Metal sees and that Swift can also write to. There is a great Swift sample project - https://github.com/MetalKit/metal/tree/master/raytracing from Marius Horga which explores one way to set up some triangle meshes. He recreates a project from Apple which was written in Objective C. Metal has really developed since then and you can do a phenomenal amount of path tracing with very few pipelines.
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’21